// index.js
var http = require('../../utils/http');
var api = require('../../utils/api');

// 获取应用实例
const app = getApp()

Page({
    data: {
        //轮播图配置
        autoplay: true,
        interval: 3000,
        duration: 1200,
        lunboData: [],
        child_id: "",
        childInfo: {},
        child: [],
        showSelectPopup: false,
        radioId: "",
        user_id: "",
        showPopup: false,
        name: "",
        idCard: "",
        errorFlag: false,
        errorIdCardFlag: false,
        none: true,
        daInfo: "",
        showIfVipPopup: false, //不是会员出现的弹窗
        docList: [],
        telephone: "",
        scrollTop: 0
    },
    /** 获取轮播 */
    getAdvertise() {
        http.getHttp(api.getAdvertise, {},
            (result) => {
                if (result.code == 0) {
                    result.data.forEach(ele => {
                        ele.imgurl = http.report_host + '/getFile/' + ele.file_id
                    })
                    this.setData({
                        lunboData: result.data
                    })
                }
            },
            (err) => {
                console.log(err);
            }, {
                hideLoading: true
            }
        )
    },
    init() {
        const child = wx.getStorageSync('child')
        const childInfo = wx.getStorageSync('childInfo')
        const userInfo = wx.getStorageSync('userInfo')
        this.setData({
            child_id: childInfo.id ? childInfo.id : 0,
            childInfo: childInfo,
            child: child,
            user_id: userInfo.id,
            radioId: childInfo.id,
            telephone: userInfo.telephone
        })
        this.getChildInfo()
        this.getAdvertise()
        if (this.data.idCard) {
            /** 添加孩子的时候检测有没有买产品，买了产品强制去激活权益 */
            http.postHttp(api.checkVIP, {
                    personal_id: this.data.idCard,
                    name: this.data.name
                },
                (result) => {
                    if (result.code == 0) {
                        if (result.data.if_vip) {
                            wx.navigateTo({
                                url: '/pages/activationInfo/index?childname=' + this.data.name +
                                    '&idcard=' + this.data.idCard + '&phone=' + this.data.telephone +
                                    '&from=my',
                            })
                        } else {
                            this.setData({
                                showPopup: false
                            })
                        }
                    }

                },
                (err) => {
                    console.log(err);
                }, {
                    hideLoading: true
                }
            )
        }

    },
    /** 添加孩子 */
    addChild() {
        this.setData({
            showPopup: true
        })
    },
    inputFocusFun() {
        this.setData({
            errorFlag: false,
            errorIdCardFlag: false
        })
    },
    onClosePopup() {
        this.setData({
            showPopup: false,
            errorFlag: false,
            name: "",
            idCard: "",
            errorIdCardFlag: false,
        })
    },
    /** 提交添加孩子 */
    submitAddFun() {
        if (this.data.name.trim() == '') {
            this.setData({
                errorFlag: true
            })
            return
        }
        if (this.data.idCard.trim() == '') {
            this.setData({
                errorIdCardFlag: true
            })
            return
        }
        const sfzReg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
        if (!sfzReg.test(this.data.idCard)) {
            wx.showToast({
                icon: "error",
                title: '请输入正确的身份证',
            })
            return
        }
        http.postHttp(api.addChild, {
                name: this.data.name,
                personal_id: this.data.idCard,
                user_id: this.data.user_id
            },
            (result) => {
                if (result.code == 0) {
                    this.setData({
                        showPopup: false
                    })
                    wx.setStorageSync('child', result.data)
                    if (result.data.length > 0) {
                        wx.setStorageSync('childInfo', result.data[0])
                    } else {
                        wx.setStorageSync('childInfo', {})
                    }
                    this.init()
                }
            },
            (err) => {
                console.log(err);
            },
        )
    },
    submitAddFun2() {
        this.setData({
            errorFlag: false,
            errorIdCardFlag: false,
            showPopup: false
        })
    },
    /** 跳转到档案页面 */
    jumpFileData() {
        wx.navigateTo({
            url: '/pages/archivesPage/index',
        })
    },
    /** 切换孩子 */
    changeChildFun() {
        this.setData({
            showSelectPopup: true
        })
    },
    /** 选择孩子触发 */
    onChangeRadio(e) {
        this.setData({
            radioId: e.detail.detail,
        });
    },
    selectChildFun() {
        this.data.child.forEach(ele => {
            if (this.data.radioId == ele.id) {
                wx.setStorageSync('childInfo', ele)
                this.init()
                this.setData({
                    showSelectPopup: false
                })
            }
        })
    },
    selectChildFun2() {
        this.setData({
            showSelectPopup: false
        })
        setTimeout(() => {
            this.init()
        }, 500);
    },
    onCloseIfVipPopup() {
        this.setData({
            showIfVipPopup: false
        })
    },
    onLoad(options) {
        this.getDocListFun()
    },
    getUserInfo(openid) {
        http.getHttp(api.getUserInfo + openid, {},
            (result) => {
                if (result.code == 0) {
                    wx.setStorageSync('child', result.data.child)
                    if (result.data.child.length > 0) {
                        const childInfo = wx.getStorageSync('childInfo')
                        if (childInfo.id == undefined) {
                            wx.setStorageSync('childInfo', result.data.child[0])
                        } else {
                            let arr = result.data.child
                            for (let i = 0; i < arr.length; i++) {
                                const ele = arr[i];
                                if (childInfo.if_vip != ele.if_vip && childInfo.id == ele.id) {
                                    wx.setStorageSync('childInfo', ele)
                                    break
                                }
                            }
                        }
                    } else {
                        wx.setStorageSync('childInfo', {})
                    }
                    wx.setStorageSync('userInfo', result.data.user)
                    this.init()
                    this.checkInfo(result)
                } else {
                    wx.clearStorage({
                        success: (res) => {
                            wx.redirectTo({
                                url: '/pages/loginHome/index',
                            })
                        },
                    })

                }
            },
            (err) => {
                console.log(err);
            },
        )

    },
    /** 跳转咨询页面 */
    jumpZixunPage(e) {
        if (!this.data.childInfo.if_vip) {
            this.setData({
                showIfVipPopup: true
            })
            return
        }
        let type = e.currentTarget.dataset.type
        switch (type) {
            case "1":
                wx.navigateTo({
                    url: '/pages/telService/index',
                })
                break;
            case "2":
                wx.navigateTo({
                    url: '/pages/needHos/index',
                })
                break;
            case "3":
                wx.navigateTo({
                    url: '/pages/makeDoc/index',
                })
                break;

            default:
                break;
        }
    },
    onCloseSelectPopup() {
        this.setData({
            showSelectPopup: false
        })
    },
    /** 获取孩子档案信息 */
    getChildInfo() {
        http.getHttp(api.getLastProfile + this.data.child_id, {},
            (result) => {
                if (result.code == 0) {
                    this.setData({
                        daInfo: result.data,
                        none: false
                    })
                } else if (result.code == -1) {
                    this.setData({
                        none: true
                    })
                }
            },
            (err) => {
                console.log(err);
            }, {
                hideLoading: true
            }
        )
    },
    /** 跳转详情页面 */
    toDetailPage(e) {
        if (!this.data.childInfo.if_vip) {
            this.setData({
                showIfVipPopup: true
            })
            return
        }
        let item = e.currentTarget.dataset.item
        wx.navigateTo({
            url: '/pages/listDetail/index?item=' + JSON.stringify(item),
        })
    },
    /** 获取专家列表 */
    getDocListFun() {
        http.getHttp(api.getDocList, {},
            (result) => {
                if (result.code == 0) {
                    result.data.forEach(ele => {
                        ele.position = ele.file_position.split(';')[0]
                    })
                    this.setData({
                        docList: result.data
                    })
                }
            },
            (err) => {
                console.log(err);
            }, {
                hideLoading: true
            }
        )
    },
    /** 处理未绑定手机号的逻辑以及绑定了手机号但是未完善信息的逻辑 */
    checkInfo(result) {
        if (result.data.user.telephone && result.data.user.if_vip) {
            if (result.data.child.length == 0) {
                wx.redirectTo({
                    url: '/pages/activationInfo/index?phone=' + result.data.user.telephone,
                })
            }
        }
    },
    onShow() {
        if (typeof this.getTabBar === 'function' &&
            this.getTabBar()) {
            this.getTabBar().setData({
                selected: 0, // 选中的tabBar
                redDot: true,
            })
        }
        const token = wx.getStorageSync('token')
        const openid = wx.getStorageSync('openid')
        if (!token) {
            wx.redirectTo({
                url: '/pages/loginHome/index',
            })
        } else {
            this.getUserInfo(openid)
        }
        this.getDocListFun()
    },
    onUnload() {

    },
    onShareAppMessage(){
        wx.showShareMenu({
            withShareTicket:true,
            menu:['shareAppMessage','shareTimeLine']
        })
    },
    onShareTimeline(){},
    onHide() {
        this.setData({
            idCard: "",
            name: ""
        })

    }
})